package com.ruoyi.web.monitor.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.session.TokenSign;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.Log;
import com.ruoyi.framework.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.Result;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.web.monitor.domain.SysUserOnline;
import com.ruoyi.web.system.service.ISysUserOnlineService;

/**
 * 在线用户监控
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/monitor/online")
public class SysUserOnlineController extends BaseController {
    @Autowired
    private ISysUserOnlineService userOnlineService;


    @SaCheckPermission("monitor:online:list")
    @GetMapping("/list")
    public TableDataInfo list(String ipaddr, String userName) {
        // 获取所有已登录的会话id
        List<String> sessionIdList = StpUtil.searchSessionId("", 0, -1, false);

        List<SysUserOnline> userOnlineList = new ArrayList<SysUserOnline>();
        for (String sessionId : sessionIdList) {
            // 根据会话id，查询对应的 SaSession 对象，此处一个 SaSession 对象即代表一个登录的账号
            SaSession session = StpUtil.getSessionBySessionId(sessionId);
            // 查询这个账号都在哪些设备登录了，依据上面的示例，账号A 的 tokenSign 数量是 3，账号B 的 tokenSign 数量是 2
            List<TokenSign> tokenSignList = session.getTokenSignList();
            System.out.println("会话id：" + sessionId + "，共在 " + tokenSignList.size() + " 设备登录");
            //
            System.out.println("session.getType()：" + session.getType());
            System.out.println("session.getLoginType()：" + session.getLoginType());
            System.out.println("session.getTimeout()：" + session.getTimeout());
            for (TokenSign item : tokenSignList) {
                System.out.println("TokenSign.getDevice()：" + item.getDevice());
                System.out.println("TokenSign.getTag()：" + item.getTag());
                System.out.println("TokenSign.getValue()：" + item.getValue());


//                if (StringUtils.isNotEmpty(ipaddr) && StringUtils.isNotEmpty(userName)) {
//                    userOnlineList.add(userOnlineService.selectOnlineByInfo(ipaddr, userName, user));
//                } else if (StringUtils.isNotEmpty(ipaddr)) {
//                    userOnlineList.add(userOnlineService.selectOnlineByIpaddr(ipaddr, user));
//                } else if (StringUtils.isNotEmpty(userName) && StringUtils.isNotNull(user.getUser())) {
//                    userOnlineList.add(userOnlineService.selectOnlineByUserName(userName, user));
//                } else {
//                    userOnlineList.add(userOnlineService.loginUserToUserOnline(user));
//                }
            }
        }
        Collections.reverse(userOnlineList);
        userOnlineList.removeAll(Collections.singleton(null));
        return getDataTable(userOnlineList);
    }

    /**
     * 强退用户
     */
    @SaCheckPermission("monitor:online:forceLogout")
    @Log(title = "在线用户", businessType = BusinessType.FORCE)
    @DeleteMapping("/{tokenId}")
    public Result forceLogout(@PathVariable String tokenId) {
        Object userId = StpUtil.getLoginId();
        //踢下线
        StpUtil.kickout(userId);
        return success();
    }
}
